{{extend "base.html"}}

{{block head}}

<script>
    $(function () {
        $('#test').treegrid({
            title:'',
            iconCls:'icon-save',
            width:'auto',
            height:'auto',
            nowrap:false,
            //rownumbers: true,
            animate:true,
            collapsible:true,
            url:'/menupost/menu',
            idField:'id',
            treeField:'menuname',
            //autoRowHeight: false,
            fit:true,
            fitColumns:true,

            columns:[
                [
                    {title:'序号', field:'id', width:50, sortable:true},
                    {title:'菜单名字', field:'menuname', width:300, editor:'text',
                        formatter:function (value) {
                            return '<span style="color:red">' + value + '</span>';
                        }
                    },
                    {field:'menuurl', title:'URL地址', width:300, editor:'text'},
                    //{field:'pid',title:'父节点',width:100,editor:'text'},
                    {field:'opt', title:'操作', width:200, align:'center',
                        formatter:function (value, rec, index) {
                            var a = '<a href="#" mce_href="#" onclick="editNode()">编辑</a> ';
                            var b = '<a href="#" mce_href="#" onclick="saveNode()">保存</a> ';
                            var c = '<a href="#" mce_href="#" onclick="cancelNode()">取消</a> ';
                            return a + b + c;
                        }
                    }
                ]
            ],
            onContextMenu:function (e, row) {
                e.preventDefault();
                $(this).treegrid('unselectAll');
                $(this).treegrid('select', row.id);
                $('#mm').menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            }
        });
    });
    function reload() {
        var node = $('#test').treegrid('getSelected');
        if (node) {
            $('#test').treegrid('reload', node.id);
        } else {
            $('#test').treegrid('reload');
        }
    }
    var codeIndex = 1000;
    function append() {
        var node = $('#test').treegrid('getSelected');
        codeIndex++;
        var data = [
            {
                id:codeIndex,
                menuname:'编辑' + codeIndex,
                menuurl:'#',
                pid:node.id,
            }
        ];

        $('#test').treegrid('append', {
            parent:(node ? node.id : null),
            data:data
        });
    }
    function remove() {
        var node = $('#test').treegrid('getSelected');
        if (node) {
            $.messager.confirm('确认', '确认删除?', function (ok) {
                if (ok) {
                    if (node.id != 1) {
                        $.ajax({
                            url:'/menu/menudel/' + node.id,
                            error:function (request) {
                                alert(request);
                            },
                            success:function () {
                                alert('删除成功');
                                $('#test').treegrid('remove', node.id);
                                $('#test').treegrid('reload');
                            }
                        });
                        //$('#test').treegrid('remove', node.id);
                        //$('#test').treegrid('reload');

                    } else {
                        alert('不允许删除Root节点')
                    }
                }
            });
        }

        /*			if (node){
         if (node.id != 1){
         $('#test').treegrid('remove', node.id);
         alert(node.id)
         }else{
         alert('不允许删除Root节点')
         }
         }*/
    }
    function editNode() {
        var node = $('#test').treegrid('getSelected');
        if (node) {
            if (node.id != 1) {
                $('#test').treegrid('beginEdit', node.id);
            } else {
                alert('不允许编辑Root节点')
            }
        }
    }
    function saveNode() {
        var node = $('#test').treegrid('getSelected');
        if (node) {
            $('#test').treegrid('endEdit', node.id);
            //post方式提交数据给后台
            $.ajax({
                url:'/menu/menusave', type:'post', data:{
                    menuid:node.id,
                    menuname:node.menuname,
                    menuurl:node.menuurl,
                    pid:node.pid
                },
                success:function () {
                    alert('保存成功');
                    //重载树
                    $('#test').treegrid('reload');

                }
            });


            //alert(node.id + "--" + node.menuname + "--" + node.menuurl + "--" + node.pid)
        }
    }
    function cancelNode() {
        var node = $('#test').treegrid('getSelected');
        if (node) {
            $('#test').treegrid('cancelEdit', node.id);
        }
    }

</script>
{{end}}

{{block content}}


<table id="test"></table>

<div id="mm" class="easyui-menu" style="width:120px;">
    <div onclick="append()">添加</div>
    <div onclick="remove()">删除</div>
</div>

{{end}}
